//
//  UserHeavyDataManager.m
//  IPUZZLE
//
//  Created by kidbaw on 3/29/13.
//  Copyright (c) 2013 kidbaw. All rights reserved.
//

#import "UserHeavyDataManager.h"

@implementation UserHeavyDataManager

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:UIImagePNGRepresentation(self.userPuzzlePicture) forKey:STRING_CODER_USER_HEAVY_DATA_USER_PUZZLE_PICTURE];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if ([decoder decodeObjectForKey:STRING_CODER_USER_HEAVY_DATA_USER_PUZZLE_PICTURE]) {
        self.userPuzzlePicture = [UIImage imageWithData:[decoder decodeObjectForKey:STRING_CODER_USER_HEAVY_DATA_USER_PUZZLE_PICTURE]];
    }
    else {
        self.userPuzzlePicture = nil;
    }
    return self;
}

- (void)save {
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self] forKey:STRING_USER_DEFAULT_USER_HEAVY_DATA_MANAGER];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

static UserHeavyDataManager *_userHeavyDataManager;
+ (UserHeavyDataManager *)Shared
{
    if (!_userHeavyDataManager) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:STRING_USER_DEFAULT_USER_HEAVY_DATA_MANAGER];
        if ([NSKeyedUnarchiver unarchiveObjectWithData:data]) {
            _userHeavyDataManager = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        
        if (_userHeavyDataManager == nil) {
            _userHeavyDataManager = [UserHeavyDataManager new];
        }
    }
    return _userHeavyDataManager;
}

@end
